package file.it.demo;

public class Test2 {
    public static int totalNumber; // 总酒数 静态变量
    public static int lastBottleNumber; // 剩余的瓶子数
    public static int lastCoverNumber; // 剩余盖子数

    public static void main(String[] args) {
        // 啤酒问题：啤酒2元一瓶，4个盖子可以换一瓶，2个空瓶可以换一瓶，请问10元可以喝多少瓶
        buy(10);
        System.out.println("总数：" + totalNumber);
        System.out.println("剩余盖子数:" + lastCoverNumber);
        System.out.println("剩余瓶子数：" + lastBottleNumber);
    }

    public static void buy(int money) {
        // 1.先买了再说
        int buyNum = money / 2;
        totalNumber += buyNum;

        // 2.把盖子和瓶子换算成钱继续买
        // 计算本轮总的盖子和瓶子数
        int allBottleNumber = buyNum + lastBottleNumber;
        int allCoverNumber = buyNum + lastCoverNumber;

        int allMoney = 0;
        if (allBottleNumber >= 2) {
            allMoney += (allBottleNumber / 2) * 2;
        }
        // 换完后剩余的瓶数
        lastBottleNumber = allBottleNumber % 2;

        if (allCoverNumber >= 4) {
            allMoney += (allCoverNumber / 4) * 2;
        }

        lastCoverNumber = allCoverNumber % 4;

        if (allMoney >= 2) {
            buy(allMoney);
        }
    }
}
